<?php

/**
 * report actions.
 *
 * @package    manageet
 * @subpackage report
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class reportActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    return sfView::SUCCESS;
  }
  
  public function executeTest(sfWebRequest $request)
  {
    var_dump(WrBasicPeer::getReportedWeeks());
  }
  
  
  
  
  
  /* actions without template - respond XML or JSON */
  
  /**
   * 
   * @param $request
   * @return unknown_type
   */
  public function executeGetLoadDistribution(sfWebRequest $request)
  {
    $this->getResponse()->setHttpHeader('Content-Type', 'application/xml; charset=utf-8');
    
    $day = $this->getRequest()->getParameter('day', null);
    $memid = $this->getRequest()->getParameter('memid', null);
    $chartType = $this->getRequest()->getParameter('chart', 'Column2D');

    $reports = ReportAnalyzer::getReports($day, $memid);
    $dist = ReportAnalyzer::getLoadDistribution($reports);
    if ($dist == null) {
      return $this->renderText('<graph></graph>');
    }
    $xml = new XmlChart($chartType);
    $xml->addSingleSet(ReportAnalyzer::getDistSet($dist,'hours', '/report/getItems'));
    return $this->renderText($xml->render());
    
  }
  
  public function executeGetItems(sfWebRequest $request)
  {
    $this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
    $day = $this->getRequest()->getParameter('day', null);
    $memid = $this->getRequest()->getParameter('memid', null);
    $cat = $this->getRequest()->getParameter('category', '');
    $items = ReportAnalyzer::getItems($cat, $day, $memid);
    return $this->renderText(json_encode($items));
  }
  
  public function executeGetIssues(sfWebRequest $request)
  {
    //$this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
    $id = $this->getRequest()->getParameter('id', null);
    $synopsis = $this->getRequest()->getParameter('synopsis', null);
    if ($id != null) {
      $ret = WrIssuePeer::getBySynopsis($id, $synopsis);
      
    } else {
      $ret = WrOthersPeer::getBySynopsis($synopsis);
    }

    $names = array();
    $dates = array();
    $comments = array();
    $hours = 0;
    if (Util::countDim($ret) == 1) {
      $names[] = $ret['name'];
      $dates[] = $ret['date1'];
      $comments[] = $ret['comment'];
      $hours += $ret['hour'];
    } else {
      foreach($ret as $value) {
        $names[] = $value['name'];
        $dates[] = $value['date1'];
        $comments[] = $value['name'].'('.$value['date1'].' '.$value['hour']. 'hours): '.$value['comment'].'<br/>';
        $hours += $value['hour'];
      }
    }
    $name = implode(',', array_unique($names));
    $date = implode(',', array_unique($dates));
    $comment = implode('', array_unique($comments));
    $issue_name = $id ? $id :$synopsis;
    $text = "[${name}] worked on ${issue_name} in [${date}] for total ${hours} hours. <br>Comments on this issue: <br/>${comment}";
    return $this->renderText($text);    
  }
  
    
  public function executeSaveChart(sfWebRequest $request)
  {
    //$this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');        
    // =============================================================================
    // ==                                processing                               ==
    // =============================================================================
    
    /**
     * Retrieve export data from POST Request sent by chart
     * Parse the Request stream into export data readable by this script
     *  
     * Store export data into an array containing keys 'stream' (contains encoded 
     * image data) ; 'meta' ( contains an array with 'width', 'height' and 'bgColor' keys) ;
     * and 'parameters' ( array of all export parameters from chart as keys, like - exportFormat, 
     * exportFileName, exportAction etc.)
     */
    $exportRequestStream = $_POST;
    $exportData = parseExportRequestStream ( $exportRequestStream );
    
    /**
     * Get the name of the export resource (php file) as per export format
     * Dynamically include the resource. The resource would process the data   
     * and perform all export related tasks
     */ 
    $exporterResource = getExporter ( $exportData ['parameters' ] [ "exportformat" ] );
    // if resource is not found terminate with error report
    
    
    if ( !@include( $exporterResource ) )  { raise_error ( 404, true );  } 
    
    /*
     * Pass export stream and meta values to the export processor & 
     * get back the export binary  
     */ 
    $exportObject = exportProcessor ( $exportData ['stream' ], $exportData ['meta' ] );
    
    /*
     * Send the export binary to output module which would either save to a server directory
     * or send the export file to download. Download terminates the process while
     * after save the output module sends back export status 
     */ 
    $exportedStatus = outputExportObject ( $exportObject, $exportData ['parameters' ] );
    
    /*
     * Build Appropriate Export Status and send back to chart by flushing the  
     * procesed status to http response. This returns status back to chart. 
     * [ This is not applicable when Download action took place ]
     */ 
    return $this->renderText(flushStatus (  $exportedStatus, $exportData ['meta'] ));
    
  }
  
}
